<template>
  <div class="login-container">
    <!-- 顶部导航栏 -->
    <van-nav-bar title="登录"  class="login_header" >
      <template #left>
        <!-- $router.back() === $router.go-1 -->
        <van-icon name="cross" @click="$router.back()" />
      </template>
    </van-nav-bar>
    <!-- 表单页面 -->
    <van-form @submit="submitFn" ref="loginFrom">
      <!-- 手机号 -->
      <van-field
      name="mobile"
      v-model="user.mobile"
      type="number"
      maxlength="11"
      :rules="userFromRules.mobile"
      placeholder="请输入手机号">
         <template #left-icon>
            <i class="toutiao toutiao-shouji"></i>
          </template>
      </van-field>
      <!-- 验证码 -->
      <van-field
      name="code"
      placeholder="请输入验证码"
      type="number"
      :rules="userFromRules.code"
      maxlength="6"
      v-model="user.code" >
         <template #left-icon>
            <i class="toutiao toutiao-yanzhengma1"></i>
          </template>
          <template #button>
            <!-- 倒计时显示 -->
            <van-count-down
            @finish="isCountDownShow = false"
            v-if="isCountDownShow"
            :time="1000 * 10" format='ss秒' />
            <!-- 验证码按钮 -->
            <van-button round
            v-else
            class="send-sms-btn"
            native-type="button"
            size="small"
            @click="onSendSms"
            type="default">发送验证码</van-button>
          </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button  block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
import { mapMutations } from 'vuex'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      userFromRules: {
        mobile: [
          { required: true, message: '手机号不能为空！' },
          { pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/, message: '手机号格式错误！' }],
        code: [
          { required: true, message: '验证码不能为空！' },
          { pattern: /^\d{6}$/, message: '验证码格式错误！' }]
      },
      isCountDownShow: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    ...mapMutations(['setUser']),
    async submitFn() {
    //  获取表单数据
      const user = this.user
      // 表单验证
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true, // 加载中禁止背景点击
        duration: 0 // 持续展示 toast 时间毫秒
      })

      // 发送请求
      try {
        const { data } = await login(user)
        // console.log('登录成功！', data)
        this.setUser(data.data)
        this.$toast({
          message: '登录成功！',
          duration: 500,
          forbidClick: true
        })
        // back返回逻辑不合理
        // this.$router.back()
        this.$router.push('/my')
      } catch (error) {
        if (error.response.status === 400) {
          this.$toast('手机号或验证码错误！')
        } else {
          this.$toast('登录失败，请稍后重试！')
        }
      }
    },
    async onSendSms() {
      try {
        await this.$refs.loginFrom.validate('mobile')
      } catch (error) {
        // 这里一定要return 否则表单验证失败依然会触发后面的代码
        return console.log('验证失败', error)
      }
      this.isCountDownShow = true
      // 3. 请求发送验证码
      try {
        await sendSms(this.user.mobile)
        this.$toast('验证码发送成功！')
      } catch (error) {
        // 发送失败，关闭倒计时
        this.isCountDownShow = false
        if (error.response.status === 429) {
          this.$toast('发送太频繁，请稍后再试！')
        } else {
          this.$toast('发送失败请稍后重试！', error)
        }
      }
    }
  }
}
</script>
<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    // width: 152px;
    padding: 12px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
